package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func CORSMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		ctx.Writer.Header().Set("Access-Control-Allow-Origin","http://localhost:8080")
		ctx.Writer.Header().Set("Access-Control-Max-Age","86400")
		ctx.Writer.Header().Set("Access-Control-Allow-Methods","*")
		ctx.Writer.Header().Set("Access-Control-Allow-Headers","*")
		ctx.Writer.Header().Set("Access-Control-Allow-Credentials","true")

		if ctx.Request.Method == http.MethodOptions { // options 请求方法 : 允许客户端查看服务器性能,查看所支持的所有请求方法
			ctx.AbortWithStatus(200)
		} else {
			ctx.Next()
		}
	}
}
